package main

import (
	"math/big"
	"strconv"
)

func myAtoi(s string) int {
	const MIN int64 = -2147483648
	const MAX int64 = 2147483647
	 ret := big.NewInt(0)
	//正负号
	flag := true
	size := len(s)
	i := 0
	if s == "" {
		return 0
	}
	//检查空格
	for ; i < size; i++ {
		if s[i] != ' ' {
			break
		}
	}

	if i >= size {
		return 0
	}
	//检查正负号;
	if s[i] == '-' || s[i] == '+' {
		if s[i] == '-' {
			flag = false
		} else {
			flag = true
		}
		i++
	}
	//检查数字
	for ; i < size; i++ {
		//不是数字就跳出
		if s[i] > '9' || s[i] < '0' {
			break
		} else {
			n, err := strconv.ParseInt(string(s[i]),10,64)
			if err != nil {
				break
			}
			ret.Mul(ret,big.NewInt(10))
			ret.Add(ret,big.NewInt(n))
		}
	}
	//添加符号
	if flag == false {
		ret.Neg(ret)
	}

	if ret.Cmp(big.NewInt(MIN)) < 0 {
		ret = big.NewInt(MIN)
	}
	if ret.Cmp(big.NewInt(MAX)) > 0 {
		ret = big.NewInt(MAX)
	}

	return int(ret.Int64())
}

func main() {
	print(myAtoi("9223372036854775808"))
}
